package cn.cnf.service.impl;

import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.Collect;
import cn.cnf.entity.Comic;
import cn.cnf.entity.Novel;
import cn.cnf.mapper.CollectionMapper;
import cn.cnf.service.CollectionService;
import cn.cnf.service.ComicService;
import cn.cnf.service.NovelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务接口实现
 *
 * @author ZhuMeiYi
 * @since 2021-02-16 09:46:41
 * @description 
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Service
@Transactional(rollbackFor = {RuntimeException.class, Error.class})
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collect> implements CollectionService {
    private final CollectionMapper collectionMapper;
    @Autowired
    private ComicService comicService;
    @Autowired
    private NovelService novelService;

    @Override
    public String addCollection(Collect collect) {
        Integer workType = collect.getWorkType();
        if (WorkType.COMIC.equals(WorkType.getByValue(workType))){
            Comic comic = comicService.getById(collect.getWorkId());
            if (comic==null){
                throw new CustomException("漫画不存在");
            }
            comicService.updateCollectNum(comic.getId(),comic.getCommentNum()+1);
        }else if (WorkType.NOVEL.equals(WorkType.getByValue(workType))){
            Novel novel = novelService.getById(collect.getWorkId());
            if (novel==null){
                throw new CustomException("小说不存在");
            }
            novelService.updateCollectNum(novel.getId(),novel.getCollectNum()+1);
        }else {
            throw new CustomException("未知的作品类型");
        }
        collect.setUsername(UserRequestUtil.getCurrentUserName());
        save(collect);
        return collect.getId();
    }

    @Override
    public String delCollection(String workId, WorkType workType) {
        if (WorkType.COMIC.equals(workType)){
            Comic comic = comicService.getById(workId);
            if (comic==null){
                throw new CustomException("漫画不存在");
            }
            comicService.updateCollectNum(comic.getId(),comic.getCommentNum()-1);
        }else if (WorkType.NOVEL.equals(workType)){
            Novel novel = novelService.getById(workId);
            if (novel==null){
                throw new CustomException("小说不存在");
            }
            novelService.updateCollectNum(novel.getId(),novel.getCollectNum()-1);
        }else {
            throw new CustomException("未知的作品类型");
        }
        String userName = UserRequestUtil.getCurrentUserName();
        LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<Collect>()
                .eq(Collect::getWorkId, workId)
                .eq(Collect::getWorkType, workType.getValue())
                .eq(Collect::getUsername, userName);
        if (!remove(wrapper)){
            throw new CustomException("取消收藏失败");
        }
        return workId;
    }

    @Override
    public List<Comic> personalComicCollect(Integer current, Integer pageSize) {
        return collectionMapper.personalComicCollect(new Page<>(current, pageSize), UserRequestUtil.getCurrentUserName());
    }

    @Override
    public List<Novel> personalNovelCollect(Integer current, Integer pageSize) {
        return collectionMapper.personalNovelCollect(new Page<>(current, pageSize), UserRequestUtil.getCurrentUserName());
    }
}